package org.myorg.v5;

import java.io.*;

public class NSGA {

	static FPeople5 best_people;


	public static String ga(int geneLength,
			int peopleNum, double p_cross,
			double p_mut, long i_num,
			int gene_type, Fc5 fc) {


		String res = "";
		String value = "";
		String time = "";
		String it = "";



		FPeople5 my_p = new FPeople5(peopleNum,geneLength,fc.get_goals_num(),gene_type);
		FEvolution5 my_e = new FEvolution5(0.1, p_cross, p_mut);
		fc.caculate(my_p.persons);
		for (int i=0; i < i_num; i++) {


			FPeople5 P = new FPeople5(my_p);
			//System.out.println(i + " generator, old people: ");
			//P.print();

			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

			my_e.crossOver(my_p);
			my_e.mutation(my_p);
			fc.caculate(my_p.persons);

			System.out.println(i + " generator, new people: ");
			my_p.print();
			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

			my_e.select(my_p,P);

			System.out.println((i+1) + " generator: ");
			my_p.print();
			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

		}

		try {
			String filePath = "./solution";

			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String g[] = new String[fc.get_goals_num()];
			for (int i = 0; i < g.length; i++) {
				g[i] = "";
			}


			for (int i = 0; i < my_p.p_num; i++) {
				//if (my_p.persons[i].rank == 0 || my_p.persons[i].rank == 1) {
				if (my_p.persons[i].rank == 0) {
					for(int j = 0; j < fc.get_goals_num(); j++) {
						g[j] = g[j] + my_p.persons[i].goals[j] + " ";	
					}

				} else {
					break;
				}


			}

			for(int j = 0; j < fc.get_goals_num(); j++) {

				myFile.println(g[j]);
			}

			myFile.close();
			resultFile.close();
		}
		catch (Exception e) {
			//message = "创建文件操作出错";
		}

		res = value + "\n" + time + "\n" + it + "\n";

		return res;
	}
	
	public static FPeople5 nsga(int geneLength,
			int peopleNum, double p_cross,
			double p_mut, long i_num,
			int gene_type, Fc5 fc) {


		String res = "";
		String value = "";
		String time = "";
		String it = "";



		FPeople5 my_p = new FPeople5(peopleNum,geneLength,fc.get_goals_num(),gene_type);
		FEvolution5 my_e = new FEvolution5(0.1, p_cross, p_mut);
		fc.caculate(my_p.persons);
		for (int i=0; i < i_num; i++) {


			FPeople5 P = new FPeople5(my_p);
			//System.out.println(i + " generator, old people: ");
			//P.print();

			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

			my_e.crossOver(my_p);
			my_e.mutation(my_p);
			fc.caculate(my_p.persons);

			//System.out.println(i + " generator, new people: ");
			//my_p.print();
			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

			my_e.select(my_p,P);

			//System.out.println((i+1) + " generator: ");
			//my_p.print();
			/*for (int ii = 0; ii < my_p.p_num; ii++) {
				for (int j = 0; j < my_p.persons[ii].goals.length; j++) {
					System.out.print(my_p.persons[ii].goals[j] + " ");	
				}
				System.out.print("\n");

			}*/

		}

		/*try {
			String filePath = "./solution1";

			File myFilePath = new File(filePath);
			if (!myFilePath.exists()) {
				myFilePath.createNewFile();
			}
			FileWriter resultFile = new FileWriter(myFilePath);
			PrintWriter myFile = new PrintWriter(resultFile);
			String g[] = new String[fc.get_goals_num()];
			for (int i = 0; i < g.length; i++) {
				g[i] = "";
			}


			for (int i = 0; i < my_p.p_num; i++) {
				//if (my_p.persons[i].rank == 0 || my_p.persons[i].rank == 1) {
				if (my_p.persons[i].rank == 0) {
					for(int j = 0; j < fc.get_goals_num(); j++) {
						g[j] = g[j] + my_p.persons[i].goals[j] + " ";	
					}

				} else {
					break;
				}


			}

			for(int j = 0; j < fc.get_goals_num(); j++) {

				myFile.println(g[j]);
			}

			myFile.close();
			resultFile.close();
		}
		catch (Exception e) {
			//message = "创建文件操作出错";
		}*/

		res = value + "\n" + time + "\n" + it + "\n";

		return my_p;
	}

	public static void main(String[] arg) {
		System.out.println("enter main");

		//double dim = 30;
		//Fc4 fc = new FFZ41(dim);
		//Fc5 fc = new F51();
		Fmap fc = new Fmap();
		//double[] data = { 0.9, 0.99, 0.999, 0.9999, 0.99999};
		//double[] data = {0.01, 0.1, 0.2, 0.4, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.999};
		//double[] data = {1.0E-12, 5.0E-11, 4.0E-11, 4.0E-11 , 2.0E-11};
		//double[] data = {1.0E12, 5.0E11, 4.0E11, 4.0E11 , 1.0E10, 1.0E09, 1.0E08, 1.0E07};
		String tmp = ga(fc.getM(), 48, 0.9, 1d/fc.getM(), 1000, fc.getN(),fc);
		System.out.println(tmp);

		System.out.println("leave main");
	}
}
